package com.qf.smsplatform.interfaces.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: LeeRui
 * 2021/2/1 下午 9:58
 * 配置延迟交换机和延迟队列
 **/
@Configuration
public class DelayedConfig {

    public static final String DELAYED_EXCHANGE = "delayed.exchange";
    public static final String DELAYED_QUEUE = "delayed.queue";
    public static final String DELAYED_ROUTING_KEY = "#";

    //Spring容器中注册交换机
    @Bean
    public Exchange delayedExchange(){
        //创建map集合存储延迟队列交换机类型信息
        Map<String,Object> args = new HashMap<>();
        args.put("x-delayed-type","topic");
        //创建交换机
        CustomExchange exchange = new CustomExchange(DELAYED_EXCHANGE,"x-delayed-message",true,false,args);
        return exchange;
    }

    //Spring容器中注册消息队列
    @Bean
    public Queue delayedQueue(){
        return QueueBuilder.durable(DELAYED_QUEUE).build();
    }

    //绑定交换机和队列
    @Bean
    public Binding delayedBinding(Queue delayedQueue,Exchange delayedExchange){
        return BindingBuilder.bind(delayedQueue).to(delayedExchange).with(DELAYED_ROUTING_KEY).noargs();
    }
}
